package team.pwy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.pwy.mapper.MemberMapper;
import team.pwy.pojo.entity.Member;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    //查询会员状态
    @Override
    public Member findByUserId(Integer userId) {
        return memberMapper.findByUserId(userId);
    }


    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨3点执行一次
    public void scheduledUpdateMemberStatus() {
        updateMemberStatus();
    }
    public void updateMemberStatus(){
        Date currentDate = new Date();
        List<Member> members = memberMapper.findAll();
        for (Member member:members) {
            if (member.getExpirationTime().before(currentDate)){
                memberMapper.updateStatus(member.getId());
            }
        }
    }
    //续费会员
    @Override
    public Integer updateByUserId(Integer userId) {
        return memberMapper.updateByUserId(userId);
    }
    //开通会员
    @Override
    public Integer addByUserId(Integer userId) {
        return memberMapper.addByUserId(userId);
    }
    //重新开通
    @Override
    public Integer updateStatus(Integer userId) {
        return memberMapper.updateOpenStatus(userId);
    }


}
